package com.orcsystem.auth.app;

import com.orcsystem.auth.Feign.MemberFeignService;
import com.orcsystem.auth.Feign.ThirdPartFeignService;
import com.orcsystem.auth.entity.vo.UserLoginVo;
import com.orcsystem.auth.entity.vo.UserRegistVo;
import com.orcsystem.auth.service.AuthService;
import com.orcsystem.common.constant.RedisConstant;
import com.orcsystem.common.utils.R;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.validation.Valid;
import java.util.HashMap;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @CreateData: 2020/12/18-15:53
 * Author   AiHongSen
 * 方法说明
 * SL 查询、DL 删除、UP 更新、IN 新增方法
 * LoginController->作用:
 */
@Controller
public class LoginController {
    @Autowired
    ThirdPartFeignService thirdPartFeignService;

    @Autowired
    AuthService authService;

    @Autowired
    StringRedisTemplate redisTemplate;

    @Autowired
    MemberFeignService memberFeignService;

    @RequestMapping("xx")
    public void test(){
        System.out.println("进入到方法");
    }

    /**
     * 发送验证码
     * @param mobile
     * @return
     */
    @ResponseBody
    @GetMapping("/sms/sendcode")
    public R sendCode(@RequestParam("mobile") String mobile){
        authService.sendCode(mobile);
        return null;
    }

    /**
     * 注册会员账号
     * @param vo
     * @param result
     * @param model
     * @param attributes
     * @return
     */
    @PostMapping("/regist")
    public String regist(@Valid UserRegistVo vo, BindingResult result, Model model, RedirectAttributes attributes){
        /**
         * 1.传入的参数 验证
         */
        // 因为login 有映射 所以 在地下 直接+ /login.html 代表在当前域名下/找
        if (result.hasErrors()){
            Map<String, String> errors = result.getFieldErrors().stream().collect(Collectors.toMap(FieldError::getField, FieldError::getDefaultMessage));
            // 因为 model.addAttribute("errors",errors); 转发不能专递值 所以 使用 RedirectAttributes 可以达到重定向传值不丢
            attributes.addFlashAttribute("errors",errors);  // 只可以取一次
            // 校验出错 返回reg.html 页
            return  "redirect:http://auth.orcsystem.com/reg.html";
        }
        /**
         * 2. 验证码 验证
         */
        // 没错误 核实验证码
        String redisValue = redisTemplate.opsForValue().get(RedisConstant.SMS_CACHE_PREFIX + vo.getMobile()).split("_")[0];
        if (StringUtils.isEmpty(redisValue) || vo.getCode().equals(redisValue)== false){
            // 验证不通过
            HashMap<String, String> errors = new HashMap<>();
            errors.put("code","验证码错误");
            attributes.addFlashAttribute("errors",errors);
            // 重定向到注册页
            return "redirect:http://auth.orcsystem.com/reg.html";
        }
        /**
         * 3. 删除Redis验证码(令牌机制)、注册
         */
        //  删除redis验证码
        redisTemplate.delete(RedisConstant.SMS_CACHE_PREFIX + vo.getMobile());
        //  调用远程会员注册
        // R r = memberFeignService.regist(vo);
        R r = new R();      // 错误
        if (r.getCode() != 0){
            // 有错误 返回错误信息
            HashMap<String, String> errors = new HashMap<>();
            attributes.addFlashAttribute("errors",errors);
            return "redirect:http://auth.orcsystem.com/reg.html";
        }
        return "redirect:http://orcsystem.com/";
    }

    @GetMapping("/login")
    public String  login(@Valid UserLoginVo vo, BindingResult result,RedirectAttributes redirectAttributes){
        System.out.println("vo = " + vo);
        // 有错误
        if (result.hasErrors()){
            Map<String, String> errors = result.getFieldErrors().stream().collect(Collectors.toMap(FieldError::getField, FieldError::getDefaultMessage));
            redirectAttributes.addFlashAttribute("errors",errors);
            return "redirect:http://auth.orcsystem.com/reg.html";
        }
        // 登录验证
        // R r = memberFeignService.login(vo);
        R r = new R();
        System.out.println("当前码值 " + r.getCode());
//        System.out.println("code " + r.getCode()+"value" + r.getData());
        int code = r.getCode();

        if (code == 0){

        }else {
            String s = "1";
            redirectAttributes.addFlashAttribute("errors",s);
            return "redirect:http://auth.orcsystem.com/reg.html";
        }
        // 通过
        return "redirect:http://orcsystem.com/";
    }


}
